using ClassDemo.Model.Entity;
using SqlSugar;


namespace ClassDemo.Repository;

/// <summary>
/// 学生Repository实现
/// </summary>
public class StudentRepository : BaseRepository<Student>, IStudentRepository
{
    public StudentRepository(SqlSugarClient db) : base(db)
    {
    }

    /// <summary>
    /// 根据学号查询学生
    /// </summary>
    /// <param name="studentNo">学号</param>
    /// <returns>学生信息</returns>
    public async Task<Student?> GetByStudentNoAsync(string studentNo)
    {
        return await _db.Queryable<Student>()
            .Where(s => s.No == studentNo)
            .FirstAsync();
    }

    /// <summary>
    /// 根据班级查询学生列表
    /// </summary>
    /// <param name="className">班级名称</param>
    /// <returns>学生列表</returns>
    public async Task<List<Student>> GetByClassAsync(string className)
    {
        return await _db.Queryable<Student>()
            .Where(s => s.Class == className)
            .ToListAsync();
    }

    /// <summary>
    /// 根据专业查询学生列表
    /// </summary>
    /// <param name="major">专业</param>
    /// <returns>学生列表</returns>
    public async Task<List<Student>> GetByMajorAsync(string major)
    {
        return await _db.Queryable<Student>()
            .Where(s => s.Major == major)
            .ToListAsync();
    }

    /// <summary>
    /// 根据年级查询学生列表
    /// </summary>
    /// <param name="grade">年级</param>
    /// <returns>学生列表</returns>
    public async Task<List<Student>> GetByGradeAsync(string grade)
    {
        return await _db.Queryable<Student>()
            .Where(s => s.Grade == grade)
            .ToListAsync();
    }

    /// <summary>
    /// 根据手机号查询学生
    /// </summary>
    /// <param name="mobile">手机号</param>
    /// <returns>学生信息</returns>
    public async Task<Student?> GetByMobileAsync(string mobile)
    {
        return await _db.Queryable<Student>()
            .Where(s => s.Mobile == mobile)
            .FirstAsync();
    }

    /// <summary>
    /// 根据学号或手机号查询学生（用于登录）
    /// </summary>
    /// <param name="account">学号或手机号</param>
    /// <returns>学生信息</returns>
    public async Task<Student?> GetByAccountAsync(string account)
    {
        return await _db.Queryable<Student>()
            .Where(s => s.No == account || s.Mobile == account)
            .FirstAsync();
    }

    /// <summary>
    /// 检查学号是否已存在
    /// </summary>
    /// <param name="studentNo">学号</param>
    /// <param name="excludeId">排除的学生ID（用于更新时检查）</param>
    /// <returns>是否存在</returns>
    public async Task<bool> IsStudentNoExistsAsync(string studentNo, int? excludeId = null)
    {
        var query = _db.Queryable<Student>().Where(s => s.No == studentNo);
        
        if (excludeId.HasValue)
        {
            query = query.Where(s => s.Id != excludeId.Value);
        }
        
        return await query.AnyAsync();
    }

    /// <summary>
    /// 检查手机号是否已存在
    /// </summary>
    /// <param name="mobile">手机号</param>
    /// <param name="excludeId">排除的学生ID（用于更新时检查）</param>
    /// <returns>是否存在</returns>
    public async Task<bool> IsMobileExistsAsync(string mobile, int? excludeId = null)
    {
        var query = _db.Queryable<Student>().Where(s => s.Mobile == mobile);
        
        if (excludeId.HasValue)
        {
            query = query.Where(s => s.Id != excludeId.Value);
        }
        
        return await query.AnyAsync();
    }
}